#include "purchasedingdanstandarditemmodel.h"
#include "user.h"

PurchaseDingdanStandardItemModel::PurchaseDingdanStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"商品型号"
         <<"商品名称"
        <<"单位"
       <<"品牌"
      <<"上限"
     <<"下限"
    <<"库存数量"
    <<"计划数量";

    setHorizontalHeaderLabels(header);
}

bool PurchaseDingdanStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role){
    if(index.column() != header.size()-1){
        return false;
    }

    DingdanDetail *detail = details[index.row()];

    //计划数量
    int planCount = value.toInt();

    if(planCount<1){
        planCount=1;
    }

    detail->count = planCount;
    QStandardItemModel::setData(index, planCount, role);

    refresh();
    return true;
}

QVariant PurchaseDingdanStandardItemModel::data(const QModelIndex &index, int role) const{
    QVariant value = QStandardItemModel::data(index, role);

    //设置内容颜色
    if (role == Qt::TextColorRole && (index.column() == header.size()-1))
        return QVariant::fromValue(QColor(Qt::blue));
    return value;
}

void PurchaseDingdanStandardItemModel::reset(){
    clear();
    historyProducts.clear();
    details.clear();
    products.clear();
    setHorizontalHeaderLabels(header);
    refresh();
}

void PurchaseDingdanStandardItemModel::insertHistoryProduct(HistoryProduct &historyProduct){
    QList<QStandardItem*> rowData;

    Product *product = historyProduct.getProduct();
    int planCount = product==nullptr?0:product->upperLimit-product->count;
    if(planCount <= 0){
        return;
    }

    rowData.append(new QStandardItem(product==nullptr?"":product->type));
    rowData.append(new QStandardItem(product==nullptr?"":product->name));
    rowData.append(new QStandardItem(product==nullptr?"":product->unit));
    rowData.append(new QStandardItem(product==nullptr?"":product->brand));
    rowData.append(new QStandardItem(product==nullptr?"0":QString("%1").arg(product->upperLimit)));
    rowData.append(new QStandardItem(product==nullptr?"0":QString("%1").arg(product->lowerLimit)));
    rowData.append(new QStandardItem(product==nullptr?"0":QString("%1").arg(product->count)));
    rowData.append(new QStandardItem(product==nullptr?"0":QString("%1").arg(planCount)));

    DingdanDetail *detail = new DingdanDetail();
    detail->username = User::getUsername();
    detail->productId = product->productId;
    detail->price = 0;
    detail->count = planCount;

    insertRow(rowCount()-1, rowData);
    historyProducts.append(&historyProduct);
    details.append(detail);
}

void PurchaseDingdanStandardItemModel::insertHistoryProduct(QList<HistoryProduct*> historyProducts){
    for(HistoryProduct* historyProduct:historyProducts){
        insertHistoryProduct(*historyProduct);
    }

    refresh();
}

void PurchaseDingdanStandardItemModel::insertProduct(Product *product){
    QList<QStandardItem*> rowData;

    int planCount = product==nullptr?0:product->upperLimit-product->count;

    rowData.append(new QStandardItem(product==nullptr?"":product->type));
    rowData.append(new QStandardItem(product==nullptr?"":product->name));
    rowData.append(new QStandardItem(product==nullptr?"":product->unit));
    rowData.append(new QStandardItem(product==nullptr?"":product->brand));
    rowData.append(new QStandardItem(product==nullptr?"0":QString("%1").arg(product->upperLimit)));
    rowData.append(new QStandardItem(product==nullptr?"0":QString("%1").arg(product->lowerLimit)));
    rowData.append(new QStandardItem(product==nullptr?"0":QString("%1").arg(product->count)));
    rowData.append(new QStandardItem(product==nullptr?"0":QString("%1").arg(planCount)));

    DingdanDetail *detail = new DingdanDetail();
    detail->username = User::getUsername();
    detail->productId = product->productId;
    detail->price = 0;
    detail->count = planCount;

    insertRow(rowCount()-1, rowData);
    products.append(product);
    details.append(detail);
}

void PurchaseDingdanStandardItemModel::insertProduct(QList<Product*> products){
    int i=0;

    for(Product *product:products){
        if(product->checkExists(this->products)){
            i++;
        }else{
            insertProduct(product);
        }
    }

    if(i!=0){
        Tools::showRepeatProductMsg();
    }
}

QList<DingdanDetail*> PurchaseDingdanStandardItemModel::getDingdanDetails(){
    return details;
}

void PurchaseDingdanStandardItemModel::refresh(){
    int totalPlanCount = 0;
    for(int i=0;i<rowCount();i++){
        QStandardItem *curItem = item(i, 7);
        if(curItem == nullptr){
            break;
        }
        int curCount = curItem->text().toInt();
        totalPlanCount+=curCount;
    }

    emit this->refreshTotalPlanCount(totalPlanCount);
}
